<?php
require_once 'modules/feedback/models/Feedback.php';
require_once 'modules/list/models/List.php';
require_once 'modules/content/models/Content.php';

class sidebarSticker extends Nine_Sticker
{
	public function run()
	{
		
	$objList = new Models_List();
        $list = $objList->getAllLists();
        
		$yahoo = array();
		$skype = array();
		$mobile = array();
		$hotmail = array();
		$mail = array();
		foreach ($list as $item) {
			if(19==$item['parent_id']){
				$yahooId = $item['custom_value'];
				$ch = curl_init("http://opi.yahoo.com/online?u=".$yahooId."&m=t");
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
				$status = curl_exec($ch);
				curl_close($ch);
				$pos = strpos($status, "NOT");
				  if($pos !== false || (null == $item['custom_value'])){
				  	$item['status'] = 0 ;
				  }
				  else{
				  	$item['status'] = 1 ;
				  }
		      
				$yahoo []= $item;
			}
			elseif(20 == $item['parent_id']){
				$skype [] = $item;
			}
			elseif (27 == $item['parent_id']){
				$hotmail [] = $item;
			}
			elseif (28 == $item['parent_id']){
				$mobile [] = $item;
			}
			elseif(29 == $item['parent_id']) {
				$mail[] = $item;
			}
		}
		
		/**
		 * Get 5 feedback its anwsered
		 */
		$objFeedback = new Models_Feedback();
		
		$allFeedbacks = $objFeedback->getAllFeedbacks(array('enabled' => 1,'finished' => 1),'feedback_id DESC',5,0);
		foreach ($allFeedbacks as &$feedback){
			$feedback['question'] = Nine_Function::subStringAtBlank($feedback['question'],85);
			$feedback['url'] = Nine_Route::_(Nine_Registry::getAppBaseUrl(). "feedback/index/detail/id/{$feedback['feedback_id']}");
		}
		unset($feedback);
		
		$this->view->allFeedbacks = $allFeedbacks;
		$this->view->yahoo = $yahoo;
		$this->view->skype = $skype;
		$this->view->hotmail = $hotmail;
		$this->view->mobile = $mobile;
		$this->view->mail = $mail;
		
		
		/**
		 * Get recruiment content
		 */
		$objContent = new Models_Content();
		$recruiment = $objContent->getContentByGid(85)->toArray();
		$recruiment['url'] = Nine_Route::_(Nine_Registry::getAppBaseUrl()."content/index/detail/id/{$recruiment['content_gid']}");
				
		$this->view->recruiment = $recruiment;
		
		
		/**
		 * Get statistic
		 */
		$this->_getStatistic();
		
	}
	
	private function _getStatistic()
	{
		$filename = "counter.txt";
		
		// Counter
		$counter = 1;
		/**
		 * Check permission write and read (or file does not exist)
		 */
		if(is_writable($filename) && is_readable($filename)){
			$counter = @file_get_contents($filename);
			$counter = $counter + 1;
			@file_put_contents($filename, $counter);
			
		} else {
			// File does not exist, create new file
			if(!file_exists($filename)){ 
				file_put_contents($filename, '1');
			}
		}
		$this->view->counter = str_pad($counter, 7, 0, STR_PAD_LEFT);
	}
}